package com.example;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button saveData = findViewById(R.id.save_data);
        Button restoreData = findViewById(R.id.restore_data);
        saveData.setOnClickListener((v) -> {
            SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
            editor.putString("name", "Tom");
            editor.putInt("age", 26);
            editor.putBoolean("married", false);
            editor.apply();
        });
        restoreData.setOnClickListener((v) -> {
            SharedPreferences preferences = getSharedPreferences("data", MODE_PRIVATE);
            String name = preferences.getString("name", "");
            int age = preferences.getInt("age", 0);
            boolean married = preferences.getBoolean("married", false);
            Log.d(TAG, "name: " + name);
            Log.d(TAG, "age: " + age);
            Log.d(TAG, "married: " + married);
        });
    }
}